package com.muyu.emaildemo.builder;

import com.muyu.emaildemo.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: SendMailBuilder
 * @Author: Zirun.Xin
 * @Date: 2025/3/26 下午4:55
 * @Version: 1.0
 * @Description: 发送邮件构建器
 */
@Component
public class SendMailBuilder {

    /**
     * 邮件发送Java集成
     */
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 模板引擎
     */
    @Autowired
    private SpringTemplateEngine templateEngine;

    /**
     * 发送简单邮件
     * @param model 基本邮件数据传输对象
     */
    public void sendSimpleMail(MailDTO model) {
        // 构建邮件内容对象
        SimpleMailMessage msg = new SimpleMailMessage();
        // 邮件发送者
        msg.setFrom(model.getSendMailAccount());
        // 邮件接收者
        msg.setTo(model.getAcceptMailAccount());
        // 邮件主题
        msg.setSubject(model.getTheme());
        // 邮件正文
        msg.setText(model.getMailText());
        // 邮件发送时间
        msg.setSentDate(model.getSendTime());
        // 邮件发送
        javaMailSender.send(msg);
    }

    /**
     * 发送带附件邮件
     * @param model 带附件邮件数据传输对象
     * @throws MessagingException 邮件发送异常
     */
    public void sendAccessoryMail(AccessoryMailDTO model) throws MessagingException {
        // 创建邮件发送帮助者
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage, true);
        // 邮件发送者
        mmh.setFrom(model.getSendMailAccount());
        // 邮件接收者
        mmh.setTo(model.getAcceptMailAccount());
        // 邮件主题
        mmh.setSubject(model.getTheme());
        // 邮件正文
        mmh.setText(model.getMailText());
        // 邮件发送时间
        mmh.setSentDate(model.getSendTime());
        // 添加附件
        mmh.addAttachment(model.getAttachmentName(), new File(model.getAttachmentPath()));
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }

    /**
     * 发送图片资源邮件
     * @param model 图片资源邮件数据传输对象
     * @throws MessagingException 邮件发送异常
     */
    public void sendImageMail(ImageMailDTO model) throws MessagingException {
        // 创建邮件发送帮助者
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage, true);

        // 邮件发送者
        mmh.setFrom(model.getSendMailAccount());
        // 邮件接收者
        mmh.setTo(model.getAcceptMailAccount());
        // 邮件主题
        mmh.setSubject(model.getTheme());
        // 邮件正文
        mmh.setText(model.getMailText(), true);
        // 邮件发送时间
        mmh.setSentDate(model.getSendTime());
        // 多张资源图片
        for (int i = 0; i < model.getContentIdList().size(); i++) {
            mmh.addInline(model.getContentIdList().get(i),
                    new FileSystemResource(new File(model.getPathList().get(i))));
        }
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }

    /**
     * 发送Thymeleaf模板邮件
     * @param model Thymeleaf模板邮件数据传输对象
     * @throws MessagingException 邮件发送异常
     */
    public void sendThymeleafMail(ThymeleafMailDTO model) throws MessagingException {
        // 创建邮件发送帮助者
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mmh = new MimeMessageHelper(mimeMessage, true);

        // 邮件发送者
        mmh.setFrom(model.getSendMailAccount());
        // 邮件接收者
        mmh.setTo(model.getAcceptMailAccount());
        // 邮件主题
        mmh.setSubject(model.getTheme());
        // 邮件发送时间
        mmh.setSentDate(model.getSendTime());
        // 邮件正文
        mmh.setText(getHtmlTextByTemplate(model.getVariableList(), model.getTemplate()), true);
        // 发送邮件
        javaMailSender.send(mimeMessage);
    }

    /**
     * 根据模板引擎获取html格式文本
     * @param variableDTOList 模板配置参数
     * @param templateName 模板文件名
     * @return html格式文本
     */
    private String getHtmlTextByTemplate(List<VariableDTO> variableDTOList, String templateName) {
        // 模板配置参数格式化成键值对
        Map<String, Object> variableDTOMap = variableDTOList.stream()
                .collect(Collectors.toMap(VariableDTO::getName, VariableDTO::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new));

        // 添加模板数据并赋值
        Context context = new Context();
        // 静态页面参数赋值
        context.setVariables(variableDTOMap);
        // 返回指定静态页面模板
        return templateEngine.process(templateName, context);
    }
}
